package com.ssd.controller;


import cn.hutool.core.bean.BeanUtil;
import com.ssd.entity.CommonUser;
import com.ssd.entity.dto.LoginForCustomerReqDTO;
import com.ssd.service.ICommonUserService;
import com.ssd.utils.UserContext;
import com.ssd.view.BaseResponse;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * <p>
 * c端用户表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-14
 */
@RestController
@RequestMapping("/common-user")
@Api(tags = "c端用户表 前端控制器")
public class CommonUserController {
    @Resource
    private ICommonUserService commonUserService;

    //登录
    @PostMapping("/login")
    @ApiOperation(value = "c端用户登录")
    public BaseResponse loginForCommonUser(@ApiParam(value = "微信授权code") @RequestParam("code") String code) throws IOException {
        return commonUserService.loginForCommonUser(code);
    }


    //更改手机号码
    @PutMapping
    @ApiOperation("更新用户手机号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phoneCode", value = "微信手机号授权码", required = true, dataTypeClass = String.class)
    })
    public BaseResponse update(@RequestParam("phoneCode") String phoneCode) {
        return commonUserService.updatePhone(phoneCode);
    }

    //修改用户信息
    @PutMapping("/updateUserInfo")
    @ApiOperation(value = "修改用户信息")
    public BaseResponse updateUserInfo(@RequestBody LoginForCustomerReqDTO reqDTO) {
        CommonUser bean = BeanUtil.toBean(reqDTO, CommonUser.class);
        return new BaseResponse(commonUserService.updateById(bean));
    }

    @GetMapping("/user")
    @ApiOperation(value = "根据用户id获取用户信息")
    public BaseResponse getUserInfo() {
        Long userId = UserContext.currentUserId();
        CommonUser userInfo = commonUserService.getById(userId);
        userInfo.setOpenId(null);
        return new  BaseResponse(userInfo);
    }
}
